home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / u2j.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  4KB  |  183 lines

  1. /*
  2.  * Take an incoming message and queue it for JNOS's SMTP.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <fcntl.h>
  13.  
  14. const char *queue = "/usr/src/jnos/spool/mqueue/";
  15.  
  16. int
  17. main(int argc, char * const * const argv)
  18. {
  19.     char fname[128], from[1024], line[1024];
  20.     const char *to, *host;
  21.     struct timeval tv;
  22.     int h, q, fm, c;
  23.     FILE *fp;
  24.     long seq;
  25.  
  26.     host = strchr((to = argv[1]), '@') + 1;
  27.     strcpy(fname, queue);
  28.     strcat(fname, "sequence.lck");
  29.     c = 0;
  30.     while ((h = open(fname, O_WRONLY|O_CREAT|O_EXCL)) == -1)
  31.     {
  32.     if (errno != EEXIST || ++c == 5)
  33.     {
  34.         perror(fname);
  35.         fprintf(stderr, "can't lock sequence file, aborting...\n");
  36.         return 1;
  37.     }
  38.     if ((fp = fopen(fname, "r")))
  39.     {
  40.         fscanf(fp, "%ld", &seq);
  41.         fclose(fp);
  42.         if (kill(seq, 0) == -1 && errno == ESRCH)
  43.         {
  44.         unlink(fname);
  45.         continue;
  46.         }
  47.     }
  48.     tv.tv_sec = 0;
  49.     tv.tv_usec = 500000;
  50.     select(0, 0, 0, 0, &tv);
  51.     }
  52.     sprintf(fname, "%10d\n", getpid());
  53.     write(h, fname, strlen(fname));
  54.     close(h);
  55.     strcpy(fname, queue);
  56.     strcat(fname, "sequence.seq");
  57.     if (!(fp = fopen(fname, "r+")))
  58.     {
  59.     perror(fname);
  60.     fprintf(stderr, "can't open sequence file, aborting...\n");
  61.     return 1;
  62.     }
  63.     fscanf(fp, "%ld", &seq);
  64.     rewind(fp);
  65.     fprintf(fp, "%ld\n", ++seq);
  66.     fclose(fp);
  67.     strcpy(fname, queue);
  68.     strcat(fname, "sequence.lck");
  69.     unlink(fname);
  70.     sprintf(fname, "%s/%ld.lck", queue, seq);
  71.     if (!(fp = fopen(fname, "w")))
  72.     {
  73.     perror(fname);
  74.     fprintf(stderr, "can't create job lock file, aborting...\n");
  75.     return 2;
  76.     }
  77.     fclose(fp);
  78.     sprintf(fname, "%s/%ld.txt", queue, seq);
  79.     if (!(fp = fopen(fname, "w")))
  80.     {
  81.     perror(fname);
  82.     fprintf(stderr, "can't create job text file, aborting...\n");
  83.     sprintf(fname, "%s/%ld.lck", queue, seq);
  84.     unlink(fname);
  85.     return 3;
  86.     }
  87.     h = 1;
  88.     while (fgets(line, sizeof line, stdin))
  89.     {
  90.     if (h == 1)
  91.     {
  92.         h = 2;
  93.         if (strncmp(line, "From ", 5) == 0)
  94.         {
  95.         for (q = 5; line[q] && line[q] != ' '; q++)
  96.             ;
  97.         line[q] = '\0';
  98.         strcpy(from, line + 5);
  99.         continue;
  100.         }
  101.     }
  102.     fputs(line, fp);
  103.     if (!h)
  104.         continue;
  105.     for (q = 0; line[q] && line[q] != ' ' && line[q] != ':'; q++)
  106.         ;
  107.     if (line[q] != ':')
  108.     {
  109.         h = 0;
  110.         continue;
  111.     }
  112.     fm = 0;
  113.     if (strncasecmp(line, "from:", 5) == 0)
  114.     {
  115.         for (q = 5; line[q] == ' ' || line[q] == '\t'; q++)
  116.         ;
  117.         strcpy(from, line + q);
  118.         for (q = 0; from[q] && from[q] != '\n'; q++)
  119.         ;
  120.         from[q] = '\0';
  121.         fm = 1;
  122.     }
  123.     for (q = 0; line[q] && line[q] != '\n'; q++)
  124.         ;
  125.     /* handle long line */
  126.     while (line[q] != '\n')
  127.     {
  128.         if (!fgets(line, sizeof line, stdin))
  129.         break;
  130.         fputs(line, fp);
  131.         if (fm)
  132.         {
  133.         strcat(from, line);
  134.         for (q = 0; from[q] && from[q] != '\n'; q++)
  135.             ;
  136.         from[q] = '\0';
  137.         }
  138.         for (q = 0; line[q] && line[q] != '\n'; q++)
  139.         ;
  140.     }
  141.     while ((c = getchar()) == ' ' || c == '\t')
  142.     {
  143.         /* RFC822 continuation */
  144.         ungetc(c, stdin);
  145.         if (!fgets(line, sizeof line, stdin))
  146.         break;
  147.         fputs(line, fp);
  148.         for (q = 0; line[q] && line[q] != '\n'; q++)
  149.         ;
  150.         while (line[q] != '\n')
  151.         {
  152.         if (!fgets(line, sizeof line, stdin))
  153.             break;
  154.         fputs(line, fp);
  155.         for (q = 0; line[q] && line[q] != '\n'; q++)
  156.             ;
  157.         }
  158.     }
  159.     if (c == EOF)
  160.         break;
  161.     ungetc(c, stdin);
  162.     }
  163.     fclose(fp);
  164.     sprintf(fname, "%s/%ld.wrk", queue, seq);
  165.     if (!(fp = fopen(fname, "w")))
  166.     {
  167.     perror(fname);
  168.     fprintf(stderr, "can't create job work file, aborting...\n");
  169.     sprintf(fname, "%s/%ld.txt", queue, seq);
  170.     unlink(fname);
  171.     sprintf(fname, "%s/%ld.lck", queue, seq);
  172.     unlink(fname);
  173.     return 4;
  174.     }
  175.     fprintf(fp, "%s\n", host);
  176.     fprintf(fp, "%s\n", from);
  177.     fprintf(fp, "%s\n", argv[1]);
  178.     fclose(fp);
  179.     sprintf(fname, "%s/%ld.lck", queue, seq);
  180.     unlink(fname);
  181.     return 0;
  182. }
  183.